using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Media;
using MCI;
using WMPLib;

namespace Emlyn.Applications.LochAlarm
{
    public partial class AlarmForm : Form
    {
        public AlarmForm()
        {
            InitializeComponent();
        }

        bool _isTest = false;
        int _alarmLengthMins = 60;

        public void Initialize(string aAlarmName, bool aIsPlaylist, bool aIsTest, int aAlarmLengthMins)
        {
            //_player = new SoundPlayer(aAlarmSoundFileName);

            // ejo replaced for media player control
            //_mci = new Media();
            //_mci.Open(aAlarmSoundFileName);
            //_mci.Repeat = true;

            wmPlayer.settings.setMode("loop", true);
            wmPlayer.settings.setMode("shuffle", true);

            if (aAlarmLengthMins > 0)
                _alarmLengthMins = aAlarmLengthMins;

            if (aIsPlaylist)
            {
                wmPlayer.currentPlaylist.clear();

                IWMPPlaylist lplaylist = wmPlayer.mediaCollection.getAll();

                for (int t = 0; t < lplaylist.count; t++)
                {
                    if (lplaylist.get_Item(t).name == aAlarmName)
                        wmPlayer.currentPlaylist.appendItem(lplaylist.get_Item(t));
                }
            }
            else
            {
                wmPlayer.URL = aAlarmName;
            }

            DoInitialize(aIsTest);
        }

        private void DoInitialize(bool aIsTest)
        {
            _isTest = aIsTest;

            if (_isTest)
            {
                this.Text = "Testing Alarm";
                this.btnSleep.Text = "Close";

                //StartPlaying();
            //}
            //else
            //{
                _alarmTime = DateTime.Now;
                _stopAlarmTime = _alarmTime.Add(new TimeSpan(0, _alarmLengthMins, 0));

                timerAlarmSound.Enabled = true;
            }
        }

        DateTime _alarmTime;
        DateTime _stopAlarmTime;
        //SoundPlayer _player;
        Media _mci;

        private void btnSleep_Click(object sender, EventArgs e)
        {
            EndAlarm();

            if (_isTest)
            {
                Close();
            }
            else
            {
                _alarmTime = DateTime.Now.Add(new TimeSpan(0, 10, 0));
                _stopAlarmTime = _alarmTime.Add(new TimeSpan(0, _alarmLengthMins + 10, 0));

                if (!_isTest)
                    btnSleep.Text = "Sleeping...";
            }
        }

        private void timerAlarmSound_Tick(object sender, EventArgs e)
        {
            if (DateTime.Now > _stopAlarmTime)
            {
                EndAlarm();
                Close();
            }
            else if (DateTime.Now > _alarmTime)
            {
                BeginAlarm();
            }
        }

        private void BeginAlarm()
        {
            StartPlaying();

            // _mci.Play();
            
            if (!_isTest)
                btnSleep.Text = "Time to wake up!!!";
        }

        private void EndAlarm()
        {
            // _mci.Stop();

            StopPlaying();

        }

        private void AlarmForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //_player.Stop();
            // _mci.Stop();

            StopPlaying();
            timerAlarmSound.Enabled = false;
            
            //_mci.Close();
        }

        private void AlarmForm_KeyPress(object sender, KeyPressEventArgs e)
        {
        }

        private void AlarmForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape)
            {
                Close();
            }
            else if (e.KeyData == Keys.Space)
            {
                btnSleep_Click(sender, new EventArgs());
            }
        }

        private void StartPlaying()
        {
            wmPlayer.Ctlcontrols.play();
        }

        private void StopPlaying()
        {
            wmPlayer.Ctlcontrols.stop();
        }
    }
}